define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var orderIndex = new Vue({
                el: "#orderIndex",
                data() {
                    return {
                        orderList: [],
                        currentPage: 1,
                        totalPage: 0,
                        offset: 0,
                        limit: 10,

                        // searchStatusOption: [{
                        //     value: 'all',
                        //     label: "全部"
                        // }, {
                        //     value: '-1',
                        //     label: "已退回"
                        // }, {
                        //     value: '0',
                        //     label: "未结算"
                        // }, {
                        //     value: '1',
                        //     label: "已结算"
                        // }],
                        commissionEventOption: [{
                            value: 'all',
                            label: "全部"
                        }, {
                            value: 'payed',
                            label: "支付后结算"
                        }, {
                            value: 'confirm',
                            label: "自提后结算"
                        }, {
                            value: 'finish',
                            label: "订单完成结算"
                        }, {
                            value: 'admin',
                            label: "手动打款"
                        }],

                        
                        searchStatusOption: [{
                            value: 'all',
                            label: "全部"
                        }, {
                            value: '-1',
                            label: "已退回"
                        }, {
                            value: '0',
                            label: "待结算"
                        }, {
                            value: '1',
                            label: "已结算"
                        }],

                        searchForm: {
                            form_1_key: "order_sn",
                            form_1_value: "",

                            form_2_key: "goods_id",
                            form_2_value: "",

                            form_3_key: "buyer_id",
                            form_3_value: "",

                            form_4_key: "user_id",
                            form_4_value: "",

                            form_5_key: "store_id",
                            form_5_value: "",

                            status: "all",
                        },
                        searchFormInit: {
                            form_1_key: "order_sn",
                            form_1_value: "",

                            form_2_key: "goods_id",
                            form_2_value: "",

                            form_3_key: "buyer_id",
                            form_3_value: "",

                            form_4_key: "user_id",
                            form_4_value: "",

                            form_5_key: "store_id",
                            form_5_value: "",

                            status: "all",
                        },
                        searchOp: {
                            order_sn:'=',

                            goods_id:'=',
                            goods_title:'like',

                            buyer_id:'=',
                            buyer_nickname:'like',
                            buyer_phone:'like',

                            user_id:'=',
                            user_nickname:'like',
                            user_phone:'like',

                            store_id:'=',
                            store_name:'like',

                            status: "=",
                        },
                        goods_total: 0,
                        amount_total: 0,
                        take_total: 0,
                        taked_total: 0,
                        take_back_total: 0,
                        isAjax: false,
                        isPopoverEdit: false,
                        orderId: null,
                        isPopoverMessage: '',

                        commissionStatusOpen: null,
                        isdisabled: false
                    }
                },
                mounted() {
                    this.reqOrderList()
                },
                methods: {
                    getrefresh() {
                        window.location.reload(() => {
                            this.reqOrderList();
                        });
                    },
                    reqOrderList() {
                        var that = this;
                        that.isAjax = true;
                        let filter = {}
                        let op = {}
                        for (key in that.searchForm) {
                            if (key == 'form_1_value') {
                                if (that.searchForm[key] != '') {
                                    filter[that.searchForm.form_1_key] = that.searchForm[key];
                                }
                            } else if (key == 'form_2_value') {
                                if (that.searchForm[key] != '') {
                                    filter[that.searchForm.form_2_key] = that.searchForm[key];
                                }
                            } else if (key == 'form_3_value') {
                                if (that.searchForm[key] != '') {
                                    filter[that.searchForm.form_3_key] = that.searchForm[key];
                                }
                            } else if (key == 'form_4_value') {
                                if (that.searchForm[key] != '') {
                                    filter[that.searchForm.form_4_key] = that.searchForm[key];
                                }
                            } else if (key == 'form_5_value') {
                                if (that.searchForm[key] != '') {
                                    filter[that.searchForm.form_5_key] = that.searchForm[key];
                                }
                            }else if (key == 'status') {
                                if (that.searchForm[key] != '' && that.searchForm[key] != 'all') {
                                    filter[key] = that.searchForm[key];
                                }
                            }
                        }
                        for (key in filter) {
                            op[key] = that.searchOp[key]
                        }
                        Fast.api.ajax({
                            url: 'groupon/store/take_log/index',
                            loading: false,
                            type: 'GET',
                            data: {
                                filter: JSON.stringify(filter),
                                op: JSON.stringify(op),
                                offset: that.offset,
                                limit: that.limit,
                            }
                        }, function (ret, res) {
                            that.orderList = res.data.rows;
                            that.totalPage = res.data.total;
                            that.goods_total = res.data.total;
                            that.amount_total = res.data.delay_total;
                            that.take_total = res.data.take_total;
                            that.taked_total = res.data.taked_total;
                            that.take_back_total = res.data.take_back_total;
                            that.isAjax = false;
                            that.isdisabled = false
                            that.isPopoverEdit = null;
                            that.commissionStatusOpen = null;
                            return false;
                        }, function (ret, res) {
                            that.isAjax = false;
                        })
                    },
                    commissionLevelFilter(level) {
                        switch (level) {
                            case 0:
                                return '自购'
                                break;
                            case 1:
                                return '一级分销'
                                break;
                            case 2:
                                return '二级分销'
                                break;
                            case 3:
                                return '三级分销'
                                break;
                        }
                    },
                    goDetail(id) {
                        let that = this;
                        Fast.api.open('groupon/order/order/detail?id=' + id, "查看详情", {
                            callback() {
                                that.reqOrderList()
                            }
                        });
                    },
                    goUser(type, id) {
                        let that = this;
                        if (type == 'store') {
                            Fast.api.open('groupon/store/store/edit?id=' + id, '查看自提点', {
                                callback() {
                                    that.reqOrderList();
                                }
                            })
                        } else if (type == 'buyer') {
                            Fast.api.open('groupon/user/user/profile?id=' + id, '查看下单用户', {
                                callback() {
                                    that.reqOrderList()
                                }
                            })
                        }
                    },
                    screenEmpty() {
                        this.searchForm = JSON.parse(JSON.stringify(this.searchFormInit))
                    },
                    handleSelectionChange(val) {
                        this.multipleSelection = val;
                    },
                    //分页(更换页面显示条数)
                    handleSizeChange(val) {
                        this.offset = 0
                        this.limit = val
                        this.currentPage = 1;
                        this.reqOrderList()
                    },
                    //当前是第几页
                    handleCurrentChange(val) {
                        this.offset = (val - 1) * this.limit;
                        this.currentPage = 1;
                        this.reqOrderList()
                    },
                    changePopoverEdit(value, order_id) {
                        this.isPopoverMessage = value;
                        this.isPopoverEdit = true;
                        this.orderId = order_id
                    },
                    tableCellClassName({
                        columnIndex
                    }) {
                        if (columnIndex > 1 && 12 > columnIndex) {
                            return 'cell-center';
                        }
                        return '';
                    },
                    settlement(type, id, index, innerIndex) {
                        let that = this;
                        let requrl;
                        switch (type) {
                            case 'settle-accounts':
                                that.isdisabled = true
                                requrl = `groupon/store/take_log/runMoney/id/${id}`
                                Fast.api.ajax({
                                    url: requrl,
                                    loading: false,
                                    type: 'GET',
                                }, function (ret, res) {
                                    that.reqOrderList();
                                    that.$refs[`commission-status-${index}`].doToggle();
                                }, function (ret, res) {
                                    that.$refs[`commission-status-${index}`].doToggle();
                                    that.isdisabled = false
                                    that.isAjax = false;
                                })
                                break;
                            case 'edit':
                                Fast.api.ajax({
                                    url: `groupon/store/take_log/edit/ids/${id}`,
                                    loading: false,
                                    type: 'POST',
                                    data: {
                                        take_money: that.isPopoverMessage
                                    }
                                }, function (ret, res) {
                                    that.reqOrderList();
                                }, function (ret, res) {
                                    that.isAjax = false;
                                })
                                break;
                            case 'cancel':
                                that.isPopoverEdit = false;
                                break;
                            case 'send-back':
                                requrl = `groupon/store/take_log/backMoney/id/${id}`;
                                that.isdisabled = true
                                Fast.api.ajax({
                                    url: requrl,
                                    loading: false,
                                    type: 'GET',
                                }, function (ret, res) {
                                    that.reqOrderList();
                                    that.$refs[`commission-status-${index}`].doToggle();
                                }, function (ret, res) {
                                    that.isAjax = false;
                                    that.isdisabled = false
                                    that.$refs[`commission-status-${index}`].doToggle();
                                })
                        }
                    },
                    changecommissionStatusOper(index) {
                        this.commissionStatusOpen = index;
                    },
                    cancelcommissionStatusOper(index) {
                        this.$refs[`commission-status-${index}`].doToggle();
                        this.commissionStatusOpen = null;
                    },
                    arraySpanMethod({
                        row,
                        column,
                        rowIndex,
                        columnIndex
                    }) {
                        if (rowIndex >= 0) {
                            if (columnIndex === 0) {
                                return [0, 0];
                            } else if (columnIndex === 1) {
                                return [1, 4];
                            }
                        }
                    },
                },
            })
        },
    };
    return Controller;
});